import {
    query,
    timeListQuery,
    lifeQuery,
    memberLevelQuery,
    userGroupQuery,
    groupTabQuery,
    delGroupById,
    findGroupById
} from '../services/homePage';
import error from '../utils/tips';
import {
    parse
} from 'qs';
import LineMarker from '../components/UserImage/LineMarker';

export default {
    namespace: 'homePage',

    state: {
        count: "0",
        timeListData: [],
        lifeData: {},
        memberLevelData: [],
        userGroupData: [],
        groupTabData: [],
        gpagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        activeLineData: {},
        groupInfoData: null
    },

    subscriptions: {},

    effects: { * query({
            payload
        }, {
            select,
            call,
            put
        }) {
            const {
                data
            } = yield call(query);
            if (data) {
                yield put({
                    type: 'querySuccess',
                    payload: {
                        count: (data.data || 0).toString(),
                    }
                })
            }
        },
        * timeListQuery({
            payload
        }, {
            select,
            call,
            put
        }) {
            const {
                data
            } = yield call(timeListQuery, parse(payload));
            if (data) {
                yield put({
                    type: 'timeListQuerySuccess',
                    payload: {
                        timeListData: data.data,
                    }
                })
            }
        },
        * lifeQuery({
            payload
        }, {
            select,
            call,
            put
        }) {
            const {
                data
            } = yield call(lifeQuery, parse(payload));
            if (data) {
                let temp = {};
                data.data && data.data.map(function(item, index) {
                    temp[item.name] = item.value;
                });
                yield put({
                    type: 'lifeQuerySuccess',
                    payload: {
                        lifeData: temp
                    }
                });
            }
        },
        * memberLevelQuery({
            payload
        }, {
            select,
            call,
            put
        }) {
            const {
                data
            } = yield call(memberLevelQuery);
            if (data) {
                // const temp = [{
                //  name: 'VVIP会员(黑金卡)',
                //  total: data.data.oldMember
                // }, {
                //  name: 'VIP会员(白金卡)',
                //  total: data.data.supperMember
                // }, {
                //  name: '普通会员(金卡)',
                //  total: data.data.member
                // }, {
                //  name: '潜在会员(银卡)',
                //  total: data.data.nomalMember
                // }];
                yield put({
                    type: 'memberLevelQuerySuccess',
                    payload: {
                        memberLevelData: data.data,
                    }
                })
            }
        },
        * userGroupQuery({
            payload
        }, {
            select,
            call,
            put
        }) {
            const {
                data
            } = yield call(userGroupQuery);
            if (data) {
                yield put({
                    type: 'userGroupQuerySuccess',
                    payload: {
                        userGroupData: data.data,
                    }
                })
            }
        },
        //============================
        * groupTabQuery({
            payload
        }, {
            select,
            call,
            put
        }) {
            const {
                data
            } = yield call(groupTabQuery, parse(payload));
            if (data) {
                yield put({
                    type: 'groupTabQuerySuccess',
                    payload: {
                        groupTabData: data.data.detail,
                        gpagination: {
                            current: parseInt(data.data.page) || 1,
                            pageSize: parseInt(data.data.pageSize) || 10,
                            total: data.data.totalRecordSize
                        }
                    }
                })
            }
        },
        //===================================
        * delGroupById({
            payload
        }, {
            call,
            put
        }) {
            const {
                data
            } = yield call(delGroupById, parse(payload));

            if (data) {
                if (!data.data) {
                    error("删除失败");
                } else {
                    const {
                        data
                    } = yield call(groupTabQuery, parse("pageSize=10&pageNum=1"));
                    if (data) {
                        yield put({
                            type: 'groupTabQuerySuccess',
                            payload: {
                                groupTabData: data.data.detail,
                                gpagination: {
                                    current: parseInt(data.data.page) || 1,
                                    pageSize: parseInt(data.data.pageSize) || 10,
                                    total: data.data.totalRecordSize
                                }
                            }
                        })
                    }
                }
            } else {
                error("删除异常");
            }
        },
        //===================================
        * findGroupById({
            payload
        }, {
            call,
            put
        }) {
            const {
                data
            } = yield call(findGroupById, parse(payload));

            if (data) {
                yield put({
                    type: 'findGroupByIdSuccess',
                    payload: data.data
                })
            } else {
                error("获取信息异常");
            }
        },

    },

    reducers: {
        querySuccess(state, action) {
            return {...state,
                ...action.payload
            }
        },
        timeListQuerySuccess(state, action) {
            return {...state,
                ...action.payload
            }
        },
        lifeQuerySuccess(state, action) {
            return {...state,
                ...action.payload
            }
        },
        memberLevelQuerySuccess(state, action) {
            return {...state,
                ...action.payload
            }
        },
        userGroupQuerySuccess(state, action) {
            return {...state,
                ...action.payload
            }
        },
        groupTabQuerySuccess(state, action) {
            return {...state,
                ...action.payload
            }
        },
        findGroupByIdSuccess(state, action) {
            return {...state,
                ...action.payload,
                groupInfoData: action.payload
            }
        }
    }
}